const { ccclass, property, menu } = cc._decorator;

@ccclass
@menu("manager/Dialog")
export default class Dialog extends cc.Component {

    // onLoad () {}
    protected onOpen: Function = null
    protected onClose: Function = null
    protected nodeName: string = "bg"
    /**关闭弹窗 */
    close() {
        // soundsManager.playClickEffect()
        let node = this.node
        if (cc.isValid(node)) {
            let target = node.getChildByName(this.nodeName);
            if (target.getNumberOfRunningActions() == 0) {
                cc.tween(target)
                    .to(0.3, { scale: 0.01 }, { easing: "backIn" })
                    .call(() => {
                        this.node.scale = 0
                        if (this.onClose) {
                            this.onClose()
                        }
                    })
                    .start();
            }
        }
    }
    /**打开弹窗 */
    open(target?: cc.Event.EventTouch, nodeName: string = this.nodeName) {
        // soundsManager.playClickEffect()
        this.nodeName = nodeName
        let node = this.node
        this.node.setPosition(cc.find("Canvas/Main Camera").convertToWorldSpaceAR(cc.v2(0, 0)));
        cc.tween(node)
            .call(() => {
                node.opacity = 0;
                node.scale = 1
            })
            .delay(0)
            .call(() => {
                node.opacity = 255;
                let target = this.node.getChildByName(this.nodeName);
                target.setScale(0.01);
                cc.tween(target).to(0.3, { scale: 1 }, { easing: "backOut" })
                    .call(() => {
                        if (this.onOpen) {
                            this.onOpen()
                        }
                    })
                    .start();
            })
            .start();
    }
    // update (dt) {}
}
